/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calafie.builder.ihm.generated;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.DefaultCaret;

import calafie.builder.Builder;
import calafie.builder.Util;
import calafie.builder.ihm.modele.swing.ModeleOrdreOngletVocation;
import calafie.builder.ihm.modele.swing.ModeleVocationOngletVocation;
import calafie.builder.ihm.modele.swing.OrdreNiveau;
import calafie.builder.ihm.modele.type.Niveau;
import calafie.builder.ihm.modele.type.TypeLegalite;
import calafie.builder.ihm.modele.type.TypeVocation;
import calafie.builder.jaxb.Capacite;
import calafie.builder.jaxb.Capacites;
import calafie.builder.jaxb.Ordre;
import calafie.builder.jaxb.Vocation;


/**
 *
 * @author Dart
 */
public class PanelVocation extends javax.swing.JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = -2190645887688797560L;
    private ModeleOrdreOngletVocation modeleOrdre;
    private ModeleVocationOngletVocation modeleVocation;

    /**
     * Creates new form PanelVocation
     */
    public PanelVocation() {
        initComponents();
        initListener();
    }

    private void initListener() {
        editOrdre.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ouvrirPopOrdre(true);
            }
        });

        newOrdre.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ouvrirPopOrdre(false);
            }
        });

        deleteOrdre.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                supprimerOrdre();
            }
        });
        
        
        vocationList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                clicVocation();
            }
        });
        
        
        editVoca.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ouvrirPopUpVocation(true);

            }
        });

        newVoca.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ouvrirPopUpVocation(false);

            }
        });

        deleteVoca.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                supprimerVoca();

            }
        });
        
        typeCombo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                selectTypeVoca();

            }

        });
        
        tableOrdres.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                selection();
            }
        });
        
        copyOrdre.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                copier();
                
            }
        });
        
        
        enableButtonOrdre(false);
        descriptionLabel.setLineWrap(true);        
        descriptionLabel.setWrapStyleWord(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        labelChoix = new javax.swing.JLabel();
        typeCombo = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        vocationList = new javax.swing.JList();
        panelButton = new javax.swing.JPanel();
        newVoca = new javax.swing.JButton();
        editVoca = new javax.swing.JButton();
        deleteVoca = new javax.swing.JButton();
        paneOrdre = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        descriptionLabel =  new JTextArea();
        nomOrdreLabel = new javax.swing.JLabel();
        coutLabel = new javax.swing.JLabel();
        potLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableOrdres = new javax.swing.JTable();
        paneButtonOrdre = new javax.swing.JPanel();
        newOrdre = new javax.swing.JButton();
        editOrdre = new javax.swing.JButton();
        deleteOrdre = new javax.swing.JButton();
        copyOrdre = new JButton();
        
        descriptionLabel.setEditable(false);
        
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        modeleOrdre = new ModeleOrdreOngletVocation();
        modeleVocation = new ModeleVocationOngletVocation();
        
        
        newVoca.setText(Util.getMessage("builder.button.newe"));
        editVoca.setText(Util.getMessage("builder.button.edit"));
        deleteVoca.setText(Util.getMessage("builder.button.delete"));

        newOrdre.setText(Util.getMessage("builder.button.new"));
        copyOrdre.setText(Util.getMessage("builder.button.copy"));
        editOrdre.setText(Util.getMessage("builder.button.edit"));
        deleteOrdre.setText(Util.getMessage("builder.button.delete"));

        tableOrdres.setModel(modeleOrdre);
        
        
        labelChoix.setText(Util.getMessage("builder.popOrdre.element.type"));

        typeCombo.setModel(new DefaultComboBoxModel(TypeVocation.getModele()));

        vocationList.setModel(modeleVocation);
        jScrollPane1.setViewportView(vocationList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(labelChoix)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(typeCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(516, 516, 516))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelChoix)
                    .addComponent(typeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelButton.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout panelButtonLayout = new javax.swing.GroupLayout(panelButton);
        panelButton.setLayout(panelButtonLayout);
        panelButtonLayout.setHorizontalGroup(
            panelButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelButtonLayout.createSequentialGroup()
                .addGroup(panelButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newVoca)
                    .addComponent(editVoca)
                    .addComponent(deleteVoca))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        panelButtonLayout.setVerticalGroup(
            panelButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelButtonLayout.createSequentialGroup()
                .addComponent(newVoca)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editVoca)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteVoca))
        );

        paneOrdre.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane3.setViewportView(descriptionLabel);

        javax.swing.GroupLayout paneOrdreLayout = new javax.swing.GroupLayout(paneOrdre);
        paneOrdre.setLayout(paneOrdreLayout);
        paneOrdreLayout.setHorizontalGroup(
            paneOrdreLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneOrdreLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(paneOrdreLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .addComponent(nomOrdreLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(coutLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(potLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        paneOrdreLayout.setVerticalGroup(
            paneOrdreLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneOrdreLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(nomOrdreLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(coutLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(potLabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );


        jScrollPane2.setViewportView(tableOrdres);

        paneButtonOrdre.setBorder(javax.swing.BorderFactory.createEtchedBorder());


        javax.swing.GroupLayout paneButtonOrdreLayout = new javax.swing.GroupLayout(paneButtonOrdre);
        paneButtonOrdre.setLayout(paneButtonOrdreLayout);
        paneButtonOrdreLayout.setHorizontalGroup(
            paneButtonOrdreLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneButtonOrdreLayout.createSequentialGroup()
                .addComponent(newOrdre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(copyOrdre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editOrdre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteOrdre))
        );
        paneButtonOrdreLayout.setVerticalGroup(
            paneButtonOrdreLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneButtonOrdreLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(newOrdre)
                .addComponent(copyOrdre)
                .addComponent(editOrdre)
                .addComponent(deleteOrdre))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(paneButtonOrdre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(paneButtonOrdre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(113, 113, 113)
                                .addComponent(paneOrdre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(panelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(128, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(paneOrdre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(65, 65, 65)
                .addComponent(panelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel coutLabel;
    private javax.swing.JButton deleteVoca;
    private javax.swing.JButton deleteOrdre;
    private JTextArea descriptionLabel;
    private javax.swing.JButton editVoca;
    private javax.swing.JButton editOrdre;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel labelChoix;
    private javax.swing.JButton newOrdre;
    private javax.swing.JButton copyOrdre;
    private javax.swing.JLabel nomOrdreLabel;
    private javax.swing.JButton newVoca;
    private javax.swing.JPanel paneButtonOrdre;
    private javax.swing.JPanel paneOrdre;
    private javax.swing.JPanel panelButton;
    private javax.swing.JLabel potLabel;
    private javax.swing.JTable tableOrdres;
    private javax.swing.JComboBox typeCombo;
    private javax.swing.JList vocationList;
    // End of variables declaration//GEN-END:variables
    private PopUpOrdre popOrdre = new PopUpOrdre();
    private PopVocation popVocation = new PopVocation();
    
    
       
    private void clearOrdre() {
        descriptionLabel.setText("");
        nomOrdreLabel.setText("");
        coutLabel.setText("");
        potLabel.setText("");
    }

    public void setOrdre(Ordre ordre) {

        String titre = "";

        titre += ordre.getNom();
        if (!ordre.isLegal()) {
            String gravite = TypeLegalite.valueOf(ordre.getGravite()).getNom();
            titre += " (" + gravite + (ordre.isPolitique() ? " politique" : "") + ")";
        }

        String pot = "";
        String cout = "Coût : ";
        cout += ordre.getArgent() + "fk |";
        cout += " Fatigue: " + ordre.getPv() + " PdV ";

        if (ordre.isAutomatique()) {
            cout += " | Automatique";
        } else {
            cout += " | Difficulté: " + ordre.getDifficulte();
            pot = "Potentiel : " + ordre.getCaracteristique();
            if (ordre.getOposition().length() > 0) {
                pot += "-" + ordre.getOposition();
            }
            pot += "+" + ordre.getCompetence();
        }

        
        DefaultCaret caret = (DefaultCaret) descriptionLabel.getCaret();
        caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
        descriptionLabel.setText(ordre.getDescription());
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        nomOrdreLabel.setText(titre);
        coutLabel.setText(cout);
        potLabel.setText(pot);
    }



    private void ouvrirPopOrdre(boolean modif) {

        clearOrdre();

        if (modif) {
            int index = tableOrdres.getSelectedRow();
            if (index == -1) {
                return;
            }
            OrdreNiveau ordre = modeleOrdre.getItem(index);
            
            Niveau oldNiveau = ordre.getNiveau();
            ordre = popOrdre.ouvertureNiveau(ordre);
            
            if (ordre != null) {
                modeleOrdre.modificationOrdre(ordre, oldNiveau);
                setOrdre(ordre.getOrdre());
            }
        } else {
            OrdreNiveau ordre = popOrdre.ouvertureNiveau(null);
            if (ordre != null) {
                modeleOrdre.ajoutOrdre(ordre);
            }
        }
    }

    private void clicVocation() {

        int index = vocationList.getSelectedIndex();
        Vocation voca = modeleVocation.getItem(index);
        if (voca != null) {
            modeleOrdre.setVocation(voca);
            clearOrdre();
            enableButtonOrdre(true);
        }
    }

    private void enableButtonOrdre(boolean enable) {
        newOrdre.setEnabled(enable);
        editOrdre.setEnabled(enable);
        deleteOrdre.setEnabled(enable);
        copyOrdre.setEnabled(enable);
    }


    private void ouvrirPopUpVocation(boolean modif) {

        if (modif) {
            int index = vocationList.getSelectedIndex();
            if (index == -1) {
                return;
            }

            Vocation voca = modeleVocation.getItem(index);
            TypeVocation oldType = TypeVocation.valueOf(voca.getType());
            voca = popVocation.ouverture(voca, null);
            if (voca != null) {

                if (!TypeVocation.valueOf(voca.getType()).equals(oldType)) {
                    Builder.getInstance().getBiblio().ajoutVoca(voca, true, oldType);
                    Builder.getInstance().getBiblio().modifVocation(TypeVocation.valueOf(voca.getType()));
                }
                Builder.getInstance().getBiblio().modifVocation(oldType);
            }
        } else {
            Vocation voca = popVocation.ouverture(null, modeleVocation.getSelected());
            if (voca != null) {
                Builder.getInstance().getBiblio().ajoutVoca(voca, false, null);

                Capacites capas = new Capacites();
                voca.setCapacites(capas);
                for (Niveau niv : Niveau.values()) {
                    Capacite capa = new Capacite();
                    capa.setNiveau(niv.getNiveau());
                    capas.getCapacite().add(capa);
                }
            }
        }
    }


    private void supprimerVoca() {
        int index = vocationList.getSelectedIndex();
        modeleVocation.deleteItem(index);
        clearSelection();
    }

    private void supprimerOrdre() {
        int index = tableOrdres.getSelectedRow();
        modeleOrdre.suppressionOrdre(modeleOrdre.getItem(index));
        clearOrdre();
    }


    private void selection() {
        int index = tableOrdres.getSelectedRow();
        if (index == -1) {
            return;
        }
        OrdreNiveau ordre = modeleOrdre.getItem(index);
        if (ordre != null) {
            setOrdre(ordre.getOrdre());
        }
    }

    private void selectTypeVoca() {
        clearSelection();

        TypeVocation type = TypeVocation.values()[typeCombo.getSelectedIndex()];
        modeleVocation.setSelected(type);
    }

    private void clearSelection() {
        modeleOrdre.setVocation(null);
        enableButtonOrdre(false);
        vocationList.clearSelection();
        clearOrdre();
        
    }
    
    private void copier() {
        int index = tableOrdres.getSelectedRow();
        if (index == -1) {
            return;
        }
        OrdreNiveau ordre = modeleOrdre.getItem(index);

        modeleOrdre.ajoutOrdre(ordre.getCopy());
    }
    
}
